<?php
action_gatekeeper ( 'deleteprojectlist' );
$group_project = $vars ['group_project'];
$semester = $vars ['semester'];
$academic_year = $vars ['academic_year'];

// delete projectlist
$projectlists = elgg_get_entities ( array (
		'type' => "object",
		'subtype' => "projectlist",
		'container_guid' => $vars ['group_guid'] 
) );

foreach ( $projectlists as $projectlist ) {
	
	$ar_groupproject = explode ( ",", $projectlist->group_project );
	$ar_deadline_editted = explode ( ",", $projectlist->deadline_editted );
	$ar_deadline = explode ( ",", $projectlist->deadline );
	$ar_semester = explode ( ",", $projectlist->semester );
	$ar_academic = explode ( ",", $projectlist->academic_year );
	$classes_state = explode ( ",", $projectlist->classes_state );
	
	
	for($t = 0; $t < count ( $ar_groupproject ); $t ++) {
		$old_semester = $ar_semester [$t];
		$old_academic_year = $ar_academic [$t];
		if ($ar_groupproject [$t] == $group_project && $old_semester == $semester && $old_academic_year == $academic_year) {
			
			$str = '';
			for($i = 0; $i < count ( $classes_state ); $i ++) {
				if ($i == $t) {
					continue;
				} else {
					if ($i == 0) {
						$str .= $classes_state [$i];
					} else {
						$str .= "," . $classes_state [$i];
					}
				}
			}
			
			$projectlist->classes_state = $str;
			
			$str = '';
			for($i = 0; $i < count ( $ar_groupproject ); $i ++) {
				if ($i == $t)
					continue;
				if ($i == 0) {
					$str .= $ar_groupproject [$i];
				} else {
					$str .= "," . $ar_groupproject [$i];
				}
			}
			
			$projectlist->group_project = $str;
			
			$str = '';
			for($i = 0; $i < count ( $ar_deadline ); $i ++) {
				if ($i == $t)
					continue;
				if ($i == 0) {
					$str .= $ar_deadline [$i];
				} else {
					$str .= "," . $ar_deadline [$i];
				}
			}
			
			$projectlist->deadline = $str;
			
			$str = '';
			for($i = 0; $i < count ( $ar_semester ); $i ++) {
				if ($i == $t)
					continue;
				if ($i == 0) {
					$str .= $ar_semester [$i];
				} else {
					$str .= "," . $ar_semester [$i];
				}
			}
			
			$projectlist->semester = $str;
			
			$str = '';
			for($i = 0; $i < count ( $ar_deadline_editted ); $i ++) {
				if ($i == $t)
					continue;
				if ($i == 0) {
					$str .= $ar_deadline_editted [$i];
				} else {
					$str .= "," . $ar_deadline_editted [$i];
				}
			}
			
			$projectlist->deadline_editted = $str;
			
			$str = '';
			for($i = 0; $i < count ( $ar_academic ); $i ++) {
				if ($i == $t)
					continue;
				if ($i == 0) {
					$str .= $ar_academic [$i];
				} else {
					$str .= "," . $ar_academic [$i];
				}
			}
			
			$projectlist->academic_year = $str;
			
			$projectlist->save ();
			break;
		}
	}
	
	break;
}

$metadata_options = array (
		array (
				'name' => 'group_project',
				'value' => $group_project 
		),
		array (
				'name' => 'semester',
				'value' => $semester 
		),
		array (
				'name' => 'academic_year',
				'value' => $academic_year 
		) 
);

$options = array (
		'type' => "object",
		'subtype' => 'project',
		'container_guid' => $vars ['group_guid'],
		'metadata_name_value_pairs' => $metadata_options 
);

$projects = elgg_get_entities_from_metadata ( $options );

foreach ( $projects as $project ) {
	$project->delete ( true );
}

system_message ( "Lớp đã bị xóa cùng toàn bộ đề tài của lớp đó." );

forward ( REFERER );


